/*
 * @lc app=leetcode.cn id=1169 lang=golang
 *
 * [1169] 查询无效交易
 */
package Solutions

import (
	"sort"
	"strconv"
	"strings"
)

// @lc code=start
type Trans struct {
	ind    int
	name   string
	time   int
	amount int
	city   string
}

func NewTrans(s string, i int) *Trans {
	t := Trans{}
	spt := strings.Split(s, ",")
	t.name = spt[0]
	t.time, _ = strconv.Atoi(spt[1])
	t.amount, _ = strconv.Atoi(spt[2])
	t.city = spt[3]
	t.ind = i
	return &t
}
func invalidTransactions(transactions []string) []string {
	var trans = make([]*Trans, len(transactions))
	for i, v := range transactions {
		trans[i] = NewTrans(v, i)
	}
	sort.Slice(trans, func(i, j int) bool {
		if trans[i].name < trans[j].name {
			return true
		} else if trans[i].name == trans[j].name {
			return trans[i].time < trans[j].time
		} else {
			return false
		}
	})
	var ans = make([]string, 0, 8)
	var status = make([]bool, len(transactions))
	for i, v := range trans {
		if v.amount > 1000 && !status[i] {
			ans = append(ans, transactions[v.ind])
			status[i] = true
		}
		for j := i + 1; j < len(trans); j++ {
			if trans[j].name != trans[i].name || trans[j].time-trans[i].time > 60 {
				break
			}
			if trans[j].city != trans[i].city {
				if !status[i] {
					ans = append(ans, transactions[v.ind])
					status[i] = true
				}
				if !status[j] {
					ans = append(ans, transactions[trans[j].ind])
					status[j] = true
				}

			}

		}

	}
	return ans

}

// @lc code=end
